<template>
  <div align="center">
    <el-table :data="saos" @selection-change="getIds" style="width:80%;margin: auto 0;" border="1px">
      <el-table-column type="selection"></el-table-column>
      <el-table-column prop="saoId" label="编号" width="100px"></el-table-column>
      <el-table-column prop="saoName" label="名称" width="100px"></el-table-column>
      <el-table-column label="性别" width="100px">
        <template slot-scope="scope">
          <span v-if="scope.row.saoSex==1">男</span>
          <span v-else>女</span>
        </template>
      </el-table-column>
      <el-table-column prop="saoAlias" label="称号" width="100px"></el-table-column>
      <el-table-column prop="saoOccupation" label="武器" width="100px"></el-table-column>
      <el-table-column prop="saoAssociation" label="队伍" width="100px"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="danger" @click="toDeleteById(scope.row.saoId)">删除</el-button>
          <el-button type="warning" @click="toSelectById(scope.row.saoId)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="sizes, prev, pager, next, jumper, ->, total"
      :total="pageInfo.total"
      :page-size.sync="pageInfo.pageSize"
      :page-sizes="pageSizes"
      :current-page.sync="pageInfo.pageNum"
      :pager-count="pagerCount"
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>

    <el-button @click="showInsertForm=true" type="primary">添加</el-button>
    <el-button @click="toDeleteMany" type="primary">删除选中</el-button>

    <el-dialog :visible.sync="showUpdateForm" @close="resetSao">
      <el-form :model="sao" label-width="80px">
        <el-form-item label="名称">
          <el-input type="text" v-model="sao.saoName"/>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="sao.saoSex">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="0">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="称号">
          <el-input type="text" v-model="sao.saoAlias"/>
        </el-form-item>
        <el-form-item label="武器">
          <el-input type="text" v-model="sao.saoOccupation"/>
        </el-form-item>
        <el-form-item label="队伍">
          <el-input type="text" v-model="sao.saoAssociation"/>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" @click="toUpdateById">修改</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="showInsertForm" @close="resetSao">
      <el-form :model="sao" label-width="80px">
        <el-form-item label="名称">
          <el-input type="text" v-model="sao.saoName"/>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="sao.saoSex">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="0">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="称号">
          <el-input type="text" v-model="sao.saoAlias"/>
        </el-form-item>
        <el-form-item label="武器">
          <el-input type="text" v-model="sao.saoOccupation"/>
        </el-form-item>
        <el-form-item label="队伍">
          <el-input type="text" v-model="sao.saoAssociation"/>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" @click="toInsertSao">添加</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "ShowAllSao",
  data(){
    return{
      sao:{saoSex:1},
      saos:[],
      ids:[],
      showUpdateForm:false,
      showInsertForm:false,
      pageSizes:[2,5,10,20],//每页显示条数候选值
      currentPage:2,//当前页为第2页
      pagerCount:3,//最多显示9个页码，多余的会折叠
      pageInfo:{
        pageNum:1,
        pageSize:5
      }
    }
  },created() {
    this.selectByPage()
  },methods:{
    handleCurrentChange(pageNum){
      this.axios({
        method:"get",
        url:"/sao/"+pageNum+"/"+this.pageInfo.pageSize
      }).then(resp => {
        this.pageInfo=resp.data
        this.saos=this.pageInfo.list
      })
    },
    handleSizeChange(pageSize){
      this.axios({
        method:"get",
        url:"/sao/"+this.pageInfo.pageNum+"/"+pageSize
      }).then(resp => {
        this.pageInfo=resp.data
        this.saos=this.pageInfo.list
      })
    },
    showAll(){
      this.axios({
        method:"get",
        url:"/sao/"
      }).then(resp => {
        this.saos = resp.data
      })
    },
    getIds(values){
      this.ids=[]
      for(let i = 0;i < values.length;i++){
        this.ids.push(values[i].saoId)
      }
    },
    toDeleteById(id){
      this.$confirm("删？","删除提示",{
        confirmButtonText:"嗯",
        cancelButtonText:"点错了",
        type:"warning"
      }).then(()=>{
        this.axios({
          method:"delete",
          url:"/sao/"+id
        }).then(resp => {
          this.selectByPage()
        })
      }).catch(()=>{
        this.$message({
          type:"warning",
          message:"删除取消"
        })
      })
    },
    toSelectById(id){
      this.$confirm("改？","修改提示",{
        confirmButtonText:"嗯",
        cancelButtonText:"点错了",
        type:"warning"
      }).then(()=>{
        this.axios({
          method:"get",
          url:"/sao/"+id
        }).then(resp => {
          this.sao = resp.data
          this.showUpdateForm=true
        })
      }).catch(()=>{
        this.$message({
          type:"warning",
          message:"修改取消"
        })
      })
    },
    toUpdateById(){
      this.axios({
        method:"put",
        url:"/sao/"+id,
        data:this.sao
      }).then(resp => {
        this.selectByPage()
        this.sao={}
        this.showUpdateForm=false
      })
    },
    toInsertSao(){
      this.axios({
        method:"post",
        url:"/sao/",
        data:this.sao
      }).then(resp => {
        this.selectByPage()
        this.showInsertForm=false
        this.sao={}
        this.$message({
          type:"success",
          message:"添加成功"
        }).catch(error => {
          this.$message({
            type:"warning",
            message:"添加失败"
          })
        })
      })
    },
    resetSao(){
      this.sao={saoSex:1}
    },
    toDeleteMany(){
      this.$confirm("删？","删除提示",{
        confirmButtonText:"嗯",
        cancelButtonText:"点错了",
        type:"danger"
      }).then(()=>{
        this.axios({
          method:"delete",
          url:"/sao/",
          data:this.ids
        }).then(resp => {
          this.selectByPage()
          this.$message({
            type:"success",
            message:"删除成功"
          })
        }).catch(error => {
          this.$message({
            type:"danger",
            message:"删除失败:"+error
          })
        })
      })
    },
    selectByPage(){
      this.axios({
        method:"get",
        url:"/sao/"+this.pageInfo.pageNum+"/"+this.pageInfo.pageSize
      }).then(resp => {
        this.pageInfo=resp.data
        this.saos=this.pageInfo.list
      })
    }
  }
}
</script>

<style scoped>

</style>
